<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;


class IndexController extends Controller
{
    //后台框架
    public function box()
    {
        return view('admin/index');
    }

    //后台页面
    public function page(Request $request, $page)
    {
        switch ($page) {
            case 'index_v1':
                $data = $this->getServerInfo();
                break;
            case 'webset': //网站
                $data = config('webset');
                break;
            case 'server': //服务器
                $data = config('server');
                break;
            case 'userlist': //用户列表
                $data = DB::table('ly_user')->join('ly_info', 'info_id', '=', 'ly_info.uid')->get();
                break;
            case 'roomlist': //房间列表
                $data = DB::table('ly_room')->get();
                break;
            case 'banlist': //禁言列表
                $data = DB::table('ly_punish')->where('result', '1')->get();
                break;
            case 'blocklist': //黑名单列表
                $data = DB::table('ly_punish')->where('result', '2')->get();
                break;
            case 'newslist': //消息列表
                $data = DB::table('ly_news')->get();
                break;
            case 'lvlist': //等级列表
                $data = DB::table('ly_lv')->get();
                break;
            case 'reward':
                $data = DB::table('ly_integral')->first();
                break;
            case 'useredit': //用户修改
                if (isset($request->id) && !empty($request->id)) {
                    $data = DB::table('ly_user')->where('id', $request->id)->join('ly_info', 'info_id', 'ly_info.uid')->first();
                } else {
                    return;
                }
                break;
            case 'logout': //注销登录
                Session::flush();
                return redirect('admin/login.html');
                break;
            default:
                $data = [];
                break;
        }
        // dd($data);
        return view('admin/block/' . $page, ['data' => $data]);
    }

    //后台登录
    public function login()
    {
        $auth = Session::get('isChatLogin');

        //判断登录验证是否通过
        if ($auth === false) {
            Session::flush(); //清除登录信息
        }

        return view('admin/login', ['isAuth' => $auth]);
    }

    #获取真实内存占用
    public function getMem()
    {
        $memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2);
        return $memory;
    }

    #获取服务器信息
    public function getServerInfo()
    {
        //获取服务器操作系统
        $info = [];
        $os = explode(" ", php_uname());
        $info['os'] = $os[0];
        //获取内核版本
        if ('/' == DIRECTORY_SEPARATOR) {
            $info['core'] = $os[2];
        } else {
            $info['core'] = $os[1];
        }
        //获取PHP版本
        $info['php'] = PHP_VERSION;
        //获取服务器解释引擎
        $info['jsyq'] = $_SERVER['SERVER_SOFTWARE'];
        //获取服务器端口
        $info['port'] = $_SERVER['SERVER_PORT'];
        //获取程序绝对路径
        $info['dir'] = $_SERVER['DOCUMENT_ROOT'] ? str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) : str_replace('\\', '/', dirname(__FILE__));
        return $info;
    }
}
